package org.mspring.mlog.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mspring.mlog.dao.PostDao;
import org.mspring.mlog.entity.Catalog;
import org.mspring.mlog.entity.Link;
import org.mspring.mlog.entity.Option;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.Tag;
import org.mspring.platform.utils.StringUtils;
import org.springframework.stereotype.Repository;
@Repository
public class PostDaoImpl extends BaseDaoImpl implements PostDao {

	@Override
	public int getPageCount(Post post) {
	     // 初始化连接
        SqlSession sqlSession = getSqlSession();
        // 执行sql
		@SuppressWarnings("unchecked")
		List<Post> list = (List<Post>) sqlSession
				.selectList(
						"postWidgetAct.getPageCount",
						post);
		
		// 断开连接
		sqlSession.close();
		
		if (null == list) {
			return 0;
		} else {
			return list.size();
		}
	}

	@Override
	public List<Post> getPage(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Post> list = (List<Post>) sqlSession.selectList(
				"postWidgetAct.getPage", post);

		// 断开连接
		sqlSession.close();

		return list;
	}

	@Override
	public List<Catalog> getPostCatalog(Post p) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Catalog> list = (List<Catalog>) sqlSession.selectList(
				"postWidgetAct.getPostCatalog", p);

		// 断开连接
		sqlSession.close();

		return list;
	}

	@Override
	public List<Tag> getPostTag(Post p) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Tag> list = (List<Tag>) sqlSession.selectList(
				"postWidgetAct.getPostTag", p);

		// 断开连接
		sqlSession.close();

		return list;
	}

	@Override
	public Post getPostById(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Post> list = (List<Post>) sqlSession.selectList(
				"postWidgetAct.getPostById", post);

		// 断开连接
		sqlSession.close();
		
		if (null != list && list.size() > 0) {
			
			return (Post) list.get(0);
		}
		
		return null;

	}

	@Override
	public List<Post> urlExists(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Post> list = (List<Post>) sqlSession.selectList(
				"postWidgetAct.urlExists", post);

		// 断开连接
		sqlSession.close();
		return list;
	}

	@Override
	public void updatePost(Post post) {
        // 初始化连接
        SqlSession sqlSession = getSqlSession();
        
		sqlSession.update(
				"postWidgetAct.updatePost",
				post);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public void updateCatalogs(Post param) {
        
        // 初始化连接
        SqlSession sqlSession = getSqlSession();
        
		sqlSession.insert(
				"postWidgetAct.updateCatalogs",
				param);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public void updateTags(Post param) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		sqlSession.insert("postWidgetAct.updateTags", param);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public List<Post> selectTag(Post param) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Post> list = (List<Post>) sqlSession.selectList(
				"postWidgetAct.selectTag", param);

		// 断开连接
		sqlSession.close();
		return list;
	}

	@Override
	public void insertTag(Post param) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		sqlSession.insert("postWidgetAct.insertTag", param);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public void insertPost(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		sqlSession.insert("postWidgetAct.insertPost", post);
		// 断开连接
		sqlSession.close();
	}

	@Override
	public List<Post> getPostByTime(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Post> list = (List<Post>) sqlSession.selectList(
				"postWidgetAct.getPostByTime", post);

		// 断开连接
		sqlSession.close();
		
		return list;

	}

	@Override
	public void deleteCatalogById(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		sqlSession.delete(
				"postWidgetAct.deleteCatalogById", post);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public void deletePostById(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		sqlSession.delete(
				"postWidgetAct.deletePostById", post);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public void updatePostStatus(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		sqlSession.update(
				"postWidgetAct.updatePostStatus", post);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public Post getPostByUrl(String url) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		Post post = new Post();
		url = StringUtils.transfer(url);
		post.setUrl(url);
		// 执行sql
		List<Post> list = (List<Post>) sqlSession.selectList(
				"postWidgetAct.getPostByUrl", post);

		// 断开连接
		sqlSession.close();
		
		if (null != list && list.size() > 0) {
			
			return (Post) list.get(0);
		}
		
		return null;
	}

}
